<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class User_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据搜索名称返回符合用户的数量
	 */
	public function count_users($name){
		if(!empty($name)){//没有其他where语句
			if(is_numeric($name)){
				$this->db->where('t_user.id', $name);
			}else{
				$this->db->like('shopname', $name);
				$this->db->or_like('username', $name);
				$this->db->or_like('contact', $name);
				$this->db->or_like('address', $name);
			}
		}
		$query = $this->db->get('t_user');
		return $query->num_rows();
	}
	/*
	 * 根据搜索名称和推广员ID返回符合用户的数量
	 */
	public function count_users_by_name_promoter($name,$promoterid){
		$this->db->where('promoterid', $promoterid);
		if(!empty($name)){
			if(is_numeric($name)){
				$this->db->where('t_user.id', $name);
			}else{
				$this->db->group_start();
				$this->db->like('shopname', $name);
				$this->db->or_like('username', $name);
				$this->db->or_like('contact', $name);
				$this->db->or_like('address', $name);
				$this->db->group_end();
			}
		}
		$query = $this->db->get('t_user');
		return $query->num_rows();
	}
	/*
	 * 根据搜索名称和分页信息返回符合的用户列表
	 */
	public function find_all_users($per_page,$offset,$name){//
		$this->db->select('t_user.id,t_user.username,t_user.shopname,t_user.contact,t_user.address,t_user.cellphone,t_region.name as district,t_user.cash,t_user.iscash,t_user.balance,t_user.regist_time,t_user.early_time,t_user.late_time,t_user.promoterid,t_promoter.name as pname');
		$this->db->from('t_user');
		$this->db->join('t_promoter', 't_user.promoterid = t_promoter.id','left');
		$this->db->join('t_region', 't_user.district = t_region.id','left');
		if(!empty($name)){
			if(is_numeric($name)){
				$this->db->where('t_user.id', $name);
			}else{
				$this->db->like('shopname', $name);
				$this->db->or_like('username', $name);
				$this->db->or_like('contact', $name);
				$this->db->or_like('address', $name);
			}
		}
		$this->db->limit($per_page, $offset);
		$this->db->order_by("id", "desc");
		$query=$this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据搜索名称、分页信息、推广员ID返回符合的用户列表
	 */
	public function find_users_by_name_promoter($per_page,$offset,$name,$promoterid){//
		$this->db->select('t_user.id,t_user.username,t_user.shopname,t_user.contact,t_user.address,t_user.cellphone,t_region.name as district,t_user.regist_time');
		$this->db->from('t_user');
		$this->db->join('t_region', 't_user.district = t_region.id','left');
		$this->db->where('promoterid', $promoterid);
		if(!empty($name)){
			if(is_numeric($name)){
				$this->db->where('t_user.id', $name);
			}else{
				$this->db->group_start();
				$this->db->like('shopname', $name);
				$this->db->or_like('username', $name);
				$this->db->or_like('contact', $name);
				$this->db->or_like('address', $name);
				$this->db->group_end();
			}
		}
		$this->db->limit($per_page, $offset);
		$this->db->order_by("id", "desc");
		$query=$this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据openid得到用户
	 */
	public function find_by_openid($openid){//
		$this->db->select('t_user.id,t_user.username,t_user.shopname,t_user.contact,t_user.address,t_user.cellphone,t_region.name as district,t_user.cash,t_user.iscash,t_user.balance,t_user.regist_time,t_user.early_time,t_user.late_time,t_promoter.name as pname');
		$this->db->from('t_user');
		$this->db->join('t_oauth', 't_oauth.userid = t_user.id','left');
		$this->db->join('t_promoter', 't_user.promoterid = t_promoter.id','left');
		$this->db->join('t_region', 't_user.district = t_region.id','left');
		$this->db->where('t_oauth.openid', $openid);
		$query=$this->db->get();
		return $query->result_array();
	}
	/*
	 * 得到所有用户
	 */
	public function find_users(){//
		$query = $this->db->get('t_user');
		return $query->result_array();
	}
	/*
	 * 根据用户ID得到用户信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_user',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 更新用户信息
	 */
	public function update($id,$user){
		$this->db->where('id', $id);
		$this->db->update('t_user', $user);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据推广员ID返回用户
	 */
	public function find_by_promoter($promoter){
		$this->db->where('promoterid', $promoter);
		$query = $this->db->get('t_user');
		return $query->result_array();
	}
	/*
	 * 根据推广号返回用户
	 */
	public function find_by_spread($spread){
		$this->db->where('spread', $spread);
		$query = $this->db->get('t_user');
		return $query->row_array();
	}
	/*
	 * 根据饭店名称返回饭店信息
	 */
	public function find_by_name($name){
		$this->db->select('id,shopname,spread');
		$this->db->like('shopname', $name);
		$query=$this->db->get('t_user');
		return $query->result_array();
	}
	/*
	 * 根据注册日期得到用户信息
	 */
	public function find_by_date($date){//
		$this->db->where("DATE_FORMAT(regist_time,'%Y-%m-%d')=",$date);
		$query = $this->db->get('t_user');
		return $query->result_array();
	}
}
